import Dexie, { type EntityTable } from "dexie";
import { ConversationProps, ProviderProps } from "./types";
import { providers } from "./initData";

export const db = new Dexie("AI_chatDatabase") as Dexie & {
  conversations: EntityTable<ConversationProps, "id">;
  providers: EntityTable<ProviderProps, "id">;
};

db.version(1).stores({
  // 主键为id，且自增
  // 新增updatedAt字段，用于排序
  conversations: "++id, updatedAt",
  providers: "++id, name",
});

export const initProviders = async () => {
  const count = await db.providers.count();
  if (count === 0) {
    db.providers.bulkAdd(providers);
  }
};
